using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;


/// <summary>
/// Summary description for AdminMenuController
/// </summary>
namespace DD.Data.SQL
{
    public class AdminMenuController
    {
        public static DataSet GetActiveMenus(int ParentId)
        {
        
            SqlConnection conn = DataManager.GetConnection();
            SqlCommand GetMenus = new SqlCommand();
            GetMenus.Connection = conn;
            GetMenus.CommandText = "DD_GetActiveAdminMenus";
            GetMenus.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@Parent_ID";
            param1.Value = ParentId;
            GetMenus.Parameters.Add(param1);
            
			SqlDataAdapter da = new SqlDataAdapter( GetMenus ) ;

			DataSet results = new DataSet();
			da.Fill ( results );          
            
            return results;
           

        }

        public static void AddMenu(string MenuName, string ControlName, int MenuParent)
        {
            SqlConnection conn = DataManager.GetConnection();
            SqlCommand AddMenus = new SqlCommand();
            AddMenus.Connection = conn;
            AddMenus.CommandText = "AddMenu";
            AddMenus.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@MENU_NAME";
            param1.Value = MenuName;
            AddMenus.Parameters.Add(param1);

            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@MENU_CONTROL";
            param2.Value = ControlName;
            AddMenus.Parameters.Add(param2);

            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@Menu_PARENT";
            param3.Value = MenuParent;
            AddMenus.Parameters.Add(param3);

            conn.Open();
            AddMenus.ExecuteNonQuery();
            conn.Close();
            
        }

        public static string GetControlName(int menuID)
        {
             SqlConnection conn = DataManager.GetConnection();
            SqlCommand GetMenus = new SqlCommand();
            GetMenus.Connection = conn;
            GetMenus.CommandText = "GetControlFromID";
            GetMenus.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@MenuItemId";
            param1.Value = menuID;
            GetMenus.Parameters.Add(param1);
            
			SqlDataAdapter da = new SqlDataAdapter( GetMenus ) ;

			DataSet results = new DataSet();
			da.Fill ( results );
            string resString = "";
            try
            {
                resString = results.Tables[0].Rows[0][0].ToString();
            }
            catch
            { }
            return resString;




        }
        public static void UpdateMenu(int menuId, string menuName, string menu_control, int menuactive, int menuparent)
        {
            SqlConnection conn = DataManager.GetConnection();
            SqlCommand UpdateMenus = new SqlCommand();
            UpdateMenus.Connection = conn;
            UpdateMenus.CommandText = "DD_UPDATE_ADMIN_MENU";
            UpdateMenus.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@ID";
            param1.Value = menuId;
            param1.SqlDbType = SqlDbType.Int;
            UpdateMenus.Parameters.Add(param1);

            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@MENU_NAME";
            param2.Value = menuName;
            param2.SqlDbType = SqlDbType.NVarChar;
            UpdateMenus.Parameters.Add(param2);

            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@MENU_CONTROL";
            param3.Value = menu_control;
            param3.SqlDbType = SqlDbType.NVarChar;
            UpdateMenus.Parameters.Add(param3);

            SqlParameter param4 = new SqlParameter();
            param4.ParameterName = "@MENU_ACTIVE";
            param4.Value = menuactive;
            param4.SqlDbType = SqlDbType.Int;
            UpdateMenus.Parameters.Add(param4);

            SqlParameter param5 = new SqlParameter();
            param5.ParameterName = "@MENU_PARENT";
            param5.Value = menuparent;
            param5.SqlDbType = SqlDbType.Int;
            UpdateMenus.Parameters.Add(param5);

            conn.Open();
            UpdateMenus.ExecuteNonQuery();
            conn.Close();

        }

        public static void DeleteMenu(int id)
        {
            SqlConnection conn = DataManager.GetConnection();
            SqlCommand DeleteMenu = new SqlCommand();
            DeleteMenu.Connection = conn;
            DeleteMenu.CommandText = "DD_DELETE_ADMIN_MENU";
            DeleteMenu.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@ID";
            param1.Value = id;
            param1.SqlDbType = SqlDbType.Int;
            DeleteMenu.Parameters.Add(param1);

            conn.Open();
            DeleteMenu.ExecuteNonQuery();
            conn.Close();
            

        }
    } 
}
